import os
os.sep
# os.path.getsize 获取文件大小 若直接获取文件夹大小则返回1024b的倍数
n = 'E:\大数据和分析\python\python基础代码'
# print(os.path.getsize('E:\大数据和分析\python\python基础代码'))

"""思路 用os.work遍历所有文件大小再累加求和"""

folder_path = n
# parent 文件路径n dirs 子文件夹，files 文件
# for parent, dirs, files in os.walk(folder_path):
#     # 输出文件夹的信息
#     for dir in dirs:
#         print('parent is :', parent)
#         print('dirname is', dir)
#     # 输出文件
#     for file in files:
#         print('parent is :', parent)
#         print('filename is :', file)
#         # 文件夹完整路径
#         fullname = os.path.join(parent, file)
#         print('the full name of the file is :', fullname)
#         # getsize 单位为b
#         file_size = os.path.getsize(fullname)/1024
#         print('the file size is : %.2f KB' % file_size)

# 获取文件夹大小
full_size = 0
for parent, dirs, files in os.walk(folder_path):
    for file in files:
        file_name = os.path.join(parent, file)
        file_size = os.path.getsize(file_name)
        full_size += file_size
print('文件夹' + n + '的总大小为%.2f b' % full_size)


# full_size = 0
# ''' 显示不出结果'''
# full_size = 0
# for parent, dirs, files in os.walk(n):
#     full_size = sum(os.path.getsize(os.path.join(parent, file)) for file in files
# print('文件夹' + n + '的总大小为%.2f b' % full_size)

full_size = 0
full_size = sum(sum(os.path.getsize(os.path.join(parent, file)) for file in files) for parent, dirs, files in os.walk(n))
print('文件夹' + n + '的总大小为%.2f b' % full_size)
